var app = getApp();
// 上传图片
const upShopLogo = function (callback,url) {
  url=url||''
  wx.showActionSheet({
    itemList: ['从相册中选择', '拍照'],
    itemColor: "#f7982a",
    success: function (res) {
      if (!res.cancel) {
        if (res.tapIndex == 0) {
          chooseWxImageShop(url,'album', callback)
        } else if (res.tapIndex == 1) {
          chooseWxImageShop(url,'camera', callback)
        }
      }
    }
  })

}
// 上传图片
const chooseWxImageShop = function (url,type, callback) {
  wx.chooseImage({
    sizeType: ['original', 'compressed'],
    sourceType: [type],
    success: function (res) {
      if (res.tempFilePaths.length <= 9) {
        for (var index in res.tempFilePaths) {
          upload_file(app.globalData.urls + (url||'/index/upload'), res.tempFilePaths[index], callback);
        }
      } else {
        wx.showToast({ title: '最多上传九张' })
      }
    }

  })

}

const upload_file = function (url, filePath, callback) {
  var userAccessToken = wx.getStorageSync('userAccessToken');
  // var signature = signa.signaturetik('token=' + token, 'userAccessToken=' + userAccessToken);
  wx.uploadFile({
    url: url,
    filePath: filePath,
    name: 'file',
    header: {
      'content-type': 'multipart/form-data'
    }, // 设置请求的 header
    // formData: { 'token': token, 'userAccessToken': userAccessToken, 'signature': signature, }, // HTTP 请求中其他额外的 form data
    success: function (res) {
      if (res.statusCode == 200) {
        callback(JSON.parse(res.data));
      } else {
        wx.showToast({ title: '上传失败' })
      }
    }
  })

}





module.exports = {
  upShopLogo
}
